Exploring Terraform Outputs

Explore the output variables and check on the cluster that we created.

Viewing output file#

We’ll retrieve the nodes of the newly created Kubernetes cluster and see what we got. But, before we do that, we need to create a kubeconfig file that will provide kubectl the information on how to access the cluster. We could do that right away with aws CLI, but we’ll make it a bit more complicated.

Structure of a cluster that we'll be viewing later

To create kubeconfig, we need to know the name of the cluster and the region in which it’s running. We might have that information in our heads. But let’s imagine that’s not the case. What if we forgot it or didn’t pay attention before? That will give us a perfect opportunity to introduce yet another Terraform feature.

We can define outputs with the information we need, as long as that information is available in Terraform state. The output is as follows.

Definition of Output.tf file

We’re specifying which data should be output by Terraform. Such outputs are generated at the end of the terraform apply process. For now, we’re only interested in the outputs so we can use them to deduce the name of the cluster and the region to retrieve the credentials for kubeconfig. If we want to see all the outputs, we can simply refresh. That updates the state file with the physical resources Terraform is tracking and, more importantly, show us those outputs. We use the commands that follow.

Command to execute terraform refresh

The output is as follows.

Output of refresh command

We can see the name of the cluster and the region. But that’s not what we really need. We’re not interested in seeing that information but rather using it to construct the command that will retrieve the credentials. We can accomplish that with the terraform output command. The command that we use is terraform output cluster_name and the output is devops-catalog.

Checking the cluster#

Now we know how to retrieve the output of a single value, so let’s use that to construct the command that will retrieve the credentials.

Commands to retrieve credentials

We specified that kubeconfig should be in the current directory by exporting the environment variable KUBECONFIG. Further on, we retrieved the credentials using aws CLI. What matters, apart from the obvious need to retrieve the credentials, is that we used terraform output to retrieve the data we need and pass them to aws.

Now we should be able to check the cluster that Terraform created for us. Let’s retrieve the nodes.

The output states that “no resources” were found in default namespace. That’s expected. We retrieved the nodes of the cluster, and we got none. EKS does not allow us to access the control plane. On the other hand, we haven’t yet created worker nodes, so there are none for now.

The cluster is empty for now
Please provide values for the following:
AWS_TF_VAR_state_bucket
Not Specified...
AWS_ACCESS_KEY_ID
Not Specified...
AWS_SECRET_ACCESS_KEY
Not Specified...
/
main.sh
backend.tf
k8s-control-plane.tf
output.tf
provider.tf
storage.tf
variables.tf
terraform.tfstate
Code Playground

Creating the Control Plane

Creating Worker Nodes